package com.java.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.java.entity.ShareInfo;
import com.java.entity.event.ShareInfoEvent;
import com.java.mapper.AccountInfoMapper;
import com.java.mapper.ShareInfoMapper;
import com.java.service.ShareService;
import com.java.util.MyMap;
import com.java.util.MyTools;
import com.java.util.resp.BaseRespone;
import com.java.util.resp.TableRespone;

@Service
public class ShareServiceImpl implements ShareService {
	
	@Autowired private ShareInfoMapper shareMapper;
	
	@Autowired private AccountInfoMapper accountMapper;

	@Override
	public BaseRespone saveShare(String shareId, String userId, String title, String subTitle, String text) {
		int row = 0;
		ShareInfo info = shareMapper.selectByKey(shareId);
		if(null == info) {
			info = new ShareInfo()
			.setShareId(MyTools.getUUID())
			.setShareUserId(userId)
			.setShareTitle(title)
			.setShareSubtitle(subTitle)
			.setShareText(text)
			.setShareTime(new Date());
			row = shareMapper.insertSelective(info);
		}else {
			info.setShareTitle(title)
			.setShareSubtitle(subTitle)
			.setShareText(text);
			row = shareMapper.updateSelective(info);
		}
		return BaseRespone.result(row, "保存成功");
	}

	@Override
	public TableRespone getShareList(String userId, String title, int page, int size) {
		MyMap mm = new MyMap().put("shareUserId", userId).put("shareTitle", title);
		List<ShareInfoEvent> list = shareMapper.selectAll(mm, page, size);
		for (ShareInfoEvent shareInfoEvent : list) {
			shareInfoEvent.setAccount(accountMapper.selectByKey(shareInfoEvent.getShareUserId()));
		}
		long ll = shareMapper.selectAllCount(mm);
		return new TableRespone(list, ll);
	}

	@Override
	public BaseRespone delShare(String shareId) {
		int row = shareMapper.deleteByKey(shareId);
		return BaseRespone.result(row, "删除成功");
	}

	@Override
	public ShareInfoEvent getShare(String shareId) {
		ShareInfoEvent info = shareMapper.selectByKey(shareId);
		if(null != info) {
			info.setAccount(accountMapper.selectByKey(info.getShareUserId()));
		}
		return info;
	}

}
